package com.delos.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delos.rbac.entity.RolesDO;
import com.delos.rbac.entity.UserRoleDO;
import com.delos.rbac.mapper.RolesMapper;
import com.delos.rbac.mapper.UserRoleMapper;
import com.delos.rbac.service.IUserRoleService;
import com.delos.rbac.vo.UserRoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 用户与角色关联表
 *
 * @author zouyaowen
 * @date 2020-06-28
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleDO> implements IUserRoleService {

    @Override
    public List<UserRoleVO> getByUserId(Long userId) {
        QueryWrapper<UserRoleDO> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        List<UserRoleDO> list = this.list(query);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<UserRoleVO> userRoleList = new ArrayList<>();
        for (UserRoleDO userRoleDO : list) {
            UserRoleVO userRoleVO = new UserRoleVO();
            BeanUtils.copyProperties(userRoleDO, userRoleVO);
            userRoleList.add(userRoleVO);
        }
        return userRoleList;
    }
}
